package com.stm.bi.jobhandler;

import com.stm.bi.etl.AbstractDataEtlTask;
import com.stm.framework.dto.Response;
import com.stm.job.annotation.JobName;
import com.stm.job.handler.JobHandler;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author chenyingjun
 * @Desc
 * @Date 2024/12/01 15:33
 */
@JobName("数据ETL任务")
@Component
public class DataEtlJobhandler implements JobHandler {
    protected static final Logger log = LoggerFactory.getLogger(DataEtlJobhandler.class);
    @Resource
    private List<AbstractDataEtlTask> dataEtlTasks;

    private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 20, 30L,
            TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(10000),
            new ThreadPoolExecutor.CallerRunsPolicy());


    @Override
    public Response execute(String params) {
        dataEtlTasks.forEach(task->{
            try {
                threadPoolExecutor.execute(task);
            }catch (Exception e){
                log.error("DataEtlJobhandler.execute,error:{}",e.getMessage(),e);
            }
        });
        return Response.success();
    }
}
